package com.xysq.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.rockcent.action.CallbackListener;
import com.rockcent.model.BClientCustomerBO;
import com.rockcent.model.constant.RealNameAuthType;
import com.umeng.analytics.MobclickAgent;
import com.xysq.adapter.ViewPagerAdapter;
import com.xysq.fragment.FavoriteFragment;
import com.xysq.fragment.MyFinishOrderListFragment;
import com.xysq.fragment.MyPublishedFragment;
import com.xysq.lemon.R;
import com.xysq.util.ToastUtil;
import com.xysq.util.UserInfoKeeper;

import java.util.ArrayList;
import java.util.List;

import butterknife.InjectView;

public class MyPublishedActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @InjectView(R.id.ibtn_back)
    ImageButton backIbtn;
/*    @InjectView(R.id.btn_publish)
    Button publishBtn;*/
    @InjectView(R.id.group_tabs)
    RadioGroup tabsGroup;
    @InjectView(R.id.rbtn_publish)
    RadioButton publishRbtn;
    @InjectView(R.id.rbtn_callback)
    RadioButton callbackRbtn;
    @InjectView(R.id.rbtn_favorite)
    RadioButton favoriteRbtn;
    @InjectView(R.id.rbtn_reward)
    RadioButton rewardRbtn;
    @InjectView(R.id.view_pager)
    ViewPager viewPager;

    private MyPublishedFragment salingFragment;
    private MyPublishedFragment soldOutFragment;
    private MyFinishOrderListFragment finishedFragment;
    private FavoriteFragment favoriteFragment;
    private List<Fragment> fragmentList = new ArrayList<Fragment>();
    private ViewPagerAdapter pagerAdapter;
    private String loginName;
    private int index = 0;
    public final static String INDEX = "index";
    private boolean checkResult = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_published);

        index = getIntent().getIntExtra(INDEX,0);
        tabsGroup.setOnCheckedChangeListener(this);
        salingFragment = MyPublishedFragment.newInstance(MyPublishedFragment.TYPE_SALING);
        soldOutFragment = MyPublishedFragment.newInstance(MyPublishedFragment.TYPE_SOLD_OUT);
        finishedFragment = MyFinishOrderListFragment.newInstance(MyFinishOrderListFragment.TYPE_FINISHED, MyFinishOrderListFragment.PAGE_ORDER);
//        finishedFragment = OrderListFragment.newInstance(OrderListFragment.TYPE_FINISHED, OrderListFragment.PAGE_ORDER);
        favoriteFragment = FavoriteFragment.newInstance();
        fragmentList.add(salingFragment);
        fragmentList.add(soldOutFragment);
        fragmentList.add(favoriteFragment);
        fragmentList.add(finishedFragment);
        pagerAdapter = new ViewPagerAdapter(getSupportFragmentManager(), fragmentList);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOffscreenPageLimit(0);
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                switch (i) {
                    case 0:
                        publishRbtn.setChecked(true);
                        if(checkResult) {
                            salingFragment.reloadData();
                        }
                        checkResult = true;
                        break;
                    case 1:
                        callbackRbtn.setChecked(true);
                        if(checkResult) {
                            soldOutFragment.reloadData();
                        }
                        checkResult = true;
                        break;
                    case 2:
                        favoriteRbtn.setChecked(true);
                        checkResult = false;
                        break;
                    case 3:
                        rewardRbtn.setChecked(true);
                        checkResult = false;
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        viewPager.setCurrentItem(index);
        switch (index) {
            case 0:
                tabsGroup.check(R.id.rbtn_publish);
                break;
            case 1:
                tabsGroup.check(R.id.rbtn_callback);
                break;
            case 2:
                tabsGroup.check(R.id.rbtn_favorite);
                break;
            case 3:
                tabsGroup.check(R.id.rbtn_reward);
                break;
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rbtn_publish:
                viewPager.setCurrentItem(0);
                break;
            case R.id.rbtn_callback:
                viewPager.setCurrentItem(1);
                break;
            case R.id.rbtn_favorite:
                viewPager.setCurrentItem(2);
                break;
            case R.id.rbtn_reward:
                viewPager.setCurrentItem(3);
                break;
        }
    }

    public void onBack(View view) {
        onBackPressed();
    }

    @Override
    public void finish() {
        Intent intent = new Intent(this,MainTabActivity.class);
        intent.putExtra(MainTabActivity.INDEX,MainTabActivity.TAG_USER);
        startActivity(intent);
    }

    public void onPublish(View view) {
        loginName = UserInfoKeeper.readPhoneNumber(this);
        if (TextUtils.isEmpty(loginName)) {
            startActivity(new Intent(this, LoginActivity.class));
        } else {
            checkIdentify();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onPageStart("我的宝贝页");
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd("我的宝贝页");
        MobclickAgent.onPause(this);
    }

    //获取用户身份认证状态
    private void checkIdentify() {
        appAction.getBClientCustomer(loginName, new CallbackListener<BClientCustomerBO>() {
            @Override
            public void onSuccess(BClientCustomerBO data) {
                if (data.getIsRealnameAuth() == RealNameAuthType.UNAUTH) {//未实名认证，跳到实名认证页面
                    startActivity(new Intent(MyPublishedActivity.this, IdentifyActivity.class));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.FAIL) {
                    Intent intent = new Intent(MyPublishedActivity.this, IdentifyActivity.class);
                    intent.putExtra(IdentifyActivity.FAIL, IdentifyActivity.FAIL);
                    startActivity(intent);
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTHING) {
                    ToastUtil.showShort(MyPublishedActivity.this, getResources().getString(R.string.toast_authing));
                } else if (data.getIsRealnameAuth() == RealNameAuthType.AUTH) {
                    Intent intent = new Intent(MyPublishedActivity.this, PublishGoodsActivity.class);
                    startActivity(intent);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }
}
